home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / program / pexeccbk.txt < prev    next >
Internet Message Format  |  1994-08-27  |  39KB

  1. From terminator!umich!samsung!uunet!mcsun!unido!laura!heike!klute Mon May 14 14:07:18 EDT 1990
  2. Article 23835 of comp.sys.atari.st:
  3. Path: terminator!umich!samsung!uunet!mcsun!unido!laura!heike!klute
  4. >From: klute@heike.informatik.uni-dortmund.de (Rainer Klute)
  5. Newsgroups: comp.sys.atari.st
  6. Subject: Pexec Cookbook: Here it is
  7. Message-ID: <2156@laura.UUCP>
  8. Date: 14 May 90 06:53:57 GMT
  9. References: <A1431442603@thelake.mn.org>
  10. Sender: news@laura.UUCP
  11. Reply-To: klute@heike.informatik.uni-dortmund.de (Rainer Klute)
  12. Organization: University of Dortmund, FRG
  13. Lines: 885
  14.  
  15.  
  16. Many people asked for the Pexec Cookbook or wondered what it was. What I
  17. have and post hereby is a "preliminary version of the long-awaited Pexec
  18. cookbook". It was posted already some time ago ("those good old days")
  19. by Allan Pratt (Atari) in this newsgroup. I do not know if a newer
  20. version exists. - Appended to the Pexec Cookbook you will find some more
  21. comp.sys.atari.st articles on the subject of program calling. Hopefully
  22. you will find some useful information there!
  23.  
  24.   Dipl.-Inform. Rainer Klute      klute@heike.informatik.uni-dortmund.de
  25.   Univ. Dortmund, IRB             klute@unido.uucp, klute@unido.bitnet
  26.   Postfach 500500         |)|/    ...uunet!mcvax!unido!klute
  27. D-4600 Dortmund 50        |\|\    Tel.: +49 231 755-4663
  28.  
  29.  
  30. =================================== snip, snip
  31. ===================================
  32.  
  33.  
  34. Allan Pratt (Atari) on the subject of Pexec:-
  35.  
  36. This is in response to a request from Christian Kaernbach which I got
  37. from  BITNET: I can't reply directly to BITNET, but I'm sure other people
  38. will find this interesting, too: it's a preliminary version of the
  39. long-awaited Pexec cookbook!
  40.  
  41. In broad terms, the things you have to know about Pexec are that it
  42. starts up a process, lets it execute, then returns to the caller
  43. when that process terminates.  The "caller" -- the process which used Pexec
  44. in the first place -- has some responsibilities: it has to make memory
  45. available to the OS for allocation to the child, and it has to build
  46. up the argument string for the child.
  47.  
  48. All GEMDOS programs are started with the largest block of OS memory 
  49. allocated to them.  Except in very rare circumstances, this block
  50. is the one stretching from the end of the accessories and resident
  51. utilities to the beginning of screen memory. The point is that your
  52. program has probably been allocated ALL of free memory.  In order to
  53. make memory available for a child process, you have to SHRINK the
  54. block you own, returning the top part of it to GEMDOS.  The time to
  55. do this is when you start up.
  56.  
  57. If you use Alcyon C (from the developer's kit), you know that you
  58. always link with a file called GEMSTART.  If you've been paying 
  59. attention, you should have gotten the *new* GEMSTART from Compuserve
  60. (or from somebody else who got it): I wrote that GEMSTART.  In
  61. GEMSTART.S, there is a lot of discussion about memory models, and then
  62. a variable you set telling how much memory you want to keep or give back
  63. to the OS.  Make your choice (when in doubt, use STACK=1), assemble
  64. GEMSTART.S, call the result GEMSEXEC.O (or something), and link the
  65. programs which Pexec with that file rather than the normal GEMSTART.
  66.  
  67. Now here's a discussion of what GEMSTART has to do with respect to
  68. keeping or returning memory:
  69.  
  70. Your program is invoked with the address of its own basepage as
  71. the argument to a function (that is, at 4(sp).l).  In this basepage
  72. is the structure you can find in your documentation.  The interesting
  73. fields are HITPA (the address of first byte NOT in your TPA),
  74. BSSBASE (the first address of your bss) and BSSLEN (the length of
  75. your BSS).
  76.  
  77. Your stack pointer starts at HITPA-8 (because 8 is the length of the
  78. basepage argument and the dummy return PC on the stack).  The space from
  79. BSSBASE+BSSLEN to your SP is the "stack+heap" space.  Library malloc()
  80. calls use this space, moving a pointer called the "break" (in the
  81. variable __break, or the C variable _break if you use Alcyon C) up as it
  82. uses memory.  Your stack pointer moves down from the top as it uses
  83. memory, and if the sp and _break ever meet, you're out of memory.  In
  84. fact, if they ever come close (within a "chicken factor" of about 512
  85. bytes or 1K), malloc() will fail because it doesn't want your stack to
  86. overwrite good data. 
  87.  
  88. When a process starts, it gets *all* of memory allocated to it: from the
  89. end of any accessories or resident utilities up to the default screen
  90. memory.  If you want to use Pexec, you have to give some memory back to
  91. the OS.  You do this with the Mshrink call.  Its arguments are the
  92. address of the memory block to shrink (your basepage address) and the
  93. new size to shrink it to.  You should be sure to leave enough room above
  94. your BSS for a reasonable stack (at least 2K) plus any malloc() calls
  95. you expect to make.  Let's say you're writing "make" and you want to
  96. leave about 32K for malloc() (for your dependency structures).  Also,
  97. since make is recursive, you should leave lots of space for the stack -
  98. maybe another 16K.  The new top of memory that your program needs is:
  99.  
  100.     newtop = your bss base address + your bss size + 16K stack + 32K heap
  101.  
  102. Since your stack pointer is at the top of your CURRENT TPA, and you're about
  103. to shrink that, you'd better move your stack:
  104.  
  105.     move.l      newtop,sp
  106.  
  107. Now you want to compute your new TPA size and call Mshrink:
  108.  
  109.     move.l      newtop,d0
  110.     sub.l       basepage,d0     ; newtop-basepage is desired TPA size
  111.     move.l      d0,-(sp)        ; set up Mshrink(basepage,d0)
  112.     move.l      basepage,-(sp)
  113.     move.w      #$4a            ; fn code for Mshrink
  114.     trap        #1
  115.     add.l       #10,sp          ; clean up args
  116.  
  117. Now that you've shrunk your TPA, the OS can allocate this new memory to
  118. your child.  It can also use this memory for Malloc(), which is used
  119. occasionally by GEM VDI for blt buffers, etc.  Note that you only
  120. have to do this once, when you start up: after that, you can do as much
  121. Pexec'ing as you want.
  122.  
  123. When you want to exec a child, you build its complete filespec into one
  124. string, and its arguments into another.  The argument string is a little
  125. strange: the first character of the argument string is the length of the
  126. rest of the string!
  127.  
  128. Here is a simple system call: pass it the name of the file to execute
  129. and the argument string to use.
  130.  
  131.         long system(cmd,args)
  132.         char *cmd, *args;
  133.         {
  134.             char buf[128];
  135.  
  136.             if (strlen(args) > 126) {
  137.                 printf("argument string too long\n");
  138.                 return -1;
  139.             }
  140.             strcpy(buf+1,args);                 /* copy args to buffer+1 */
  141.             buf[0] = strlen(args);              /* set buffer[0] to len */
  142.             return Pexec(0,cmd,buf,0L);
  143.         }
  144.  
  145. The first zero in the Pexec call is the Pexec function code: load and
  146. go.  The cmd argument is the full filespec, with the path, file name,
  147. and file type.  The third argument is the command-line argument string,
  148. and the fourth argument is the environment pointer.  A null environment
  149. pointer means "let the child inherit A COPY OF my environment."
  150.  
  151. This call will load the program, pass the arguments and environment to
  152. it, and execute it.  When the program terminates, the call returns the
  153. exit code from the program.  If the Pexec fails (not enough memory, file
  154. not found, etc.) a negative code is returned, and you should deal with
  155. it accordingly.  Note that error returns from Pexec are always negative
  156. LONGS, while return codes from the child will have zeros in the upper 16 bits.
  157.  
  158. EXIT CODES:
  159.  
  160. GEMDOS, like MS-DOS before it, allows programs to return a 16-bit exit
  161. code to their parents when they terminate.  This is done with the
  162. Pterm(errcode) call.  The value in errcode is passed to the parent
  163. as the return value of the Pexec system call.  The C library function
  164. exit(errcode) usually uses this call.
  165.  
  166. Unfortunately, the people who wrote the startup file for the Alcyon C
  167. compiler didn't use this.  The compiler calls exit() with an error code,
  168. and exit() calls _exit(), but _exit always